package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourism.common.Result;
import com.tourism.entity.TourRoute;
import com.tourism.entity.UserRoute;
import com.tourism.mapper.TourRouteMapper;
import com.tourism.mapper.UserRouteMapper;
import com.tourism.service.UserRouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Slf4j
public class UserRouteServiceImpl implements UserRouteService {

    @Autowired
    private UserRouteMapper userRouteMapper;

    @Autowired
    private TourRouteMapper tourRouteMapper;

    @Override
    @Transactional
    public Result selectRoute(Integer userId, Integer routeId) {
        try {
            // 1. 验证路线是否存在
            TourRoute route = tourRouteMapper.selectById(routeId);
            if (route == null) {
                return Result.fail("路线不存在");
            }

            // 2. 查询用户是否已有选择的路线
            LambdaQueryWrapper<UserRoute> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserRoute::getUserId, userId);
            UserRoute existingRoute = userRouteMapper.selectOne(wrapper);

            // 3. 更新或新增选择
            if (existingRoute != null) {
                existingRoute.setRouteId(routeId);
                existingRoute.setUpdateTime(LocalDateTime.now());
                userRouteMapper.updateById(existingRoute);
            } else {
                UserRoute userRoute = new UserRoute();
                userRoute.setUserId(userId);
                userRoute.setRouteId(routeId);
                userRouteMapper.insert(userRoute);
            }

            return Result.success("路线选择成功");
        } catch (Exception e) {
            log.error("选择路线失败", e);
            return Result.fail("选择路线失败");
        }
    }

    @Override
    public Result getCurrentRoute(Integer userId) {
        try {
            LambdaQueryWrapper<UserRoute> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserRoute::getUserId, userId);
            UserRoute userRoute = userRouteMapper.selectOne(wrapper);

            if (userRoute != null) {
                // 获取路线详细信息
                TourRoute route = tourRouteMapper.selectById(userRoute.getRouteId());
                return Result.success(route);
            }
            return Result.success(null);
        } catch (Exception e) {
            log.error("获取当前路线失败", e);
            return Result.fail("获取当前路线失败");
        }
    }

    @Override
    @Transactional
    public Result cancelRoute(Integer userId) {
        try {
            LambdaQueryWrapper<UserRoute> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserRoute::getUserId, userId);
            userRouteMapper.delete(wrapper);
            return Result.success("取消路线成功");
        } catch (Exception e) {
            log.error("取消路线失败", e);
            return Result.fail("取消路线失败");
        }
    }
}